﻿<style>
    #TestBenchForm .layui-form-label {
        width: 100px;
    }

    #TestBenchForm .layui-input-block {
        margin-left: 140px;
    }


    #tableCheckItem + .layui-table-view .layui-table-tool-temp {
        padding-right: 0;
    }
</style>
<div class="layui-tab layui-tab-card">
    <ul class="layui-tab-title">
        <li class="layui-this">Basic</li>
        <li class=" editli">EquipList</li>
        <li class=" editli">SpotCheck</li>
    </ul>
    <div class="layui-tab-content">
        <!--Basic-->
        <div class="layui-tab-item layui-show">
            <form id="TestBenchForm" lay-filter="TestBenchForm" class="layui-form model-form  customform" style="padding-top:0px">
                <div class="model-form-body" style="padding-top:0px" onscroll="layui.admin.hideFixedEl();">
                    <div class="layui-form-item">
                        <div class="layui-inline  layui-col-md4">
                            <label class="layui-form-label layui-form-required">BenchNo</label>
                            <div class="layui-input-block">
                                <input required lay-verify="required" id="BenchNo" name="BenchNo" placeholder="BenchNo" type="text" class="layui-input" />
                            </div>
                        </div>
                        <div class="layui-inline  layui-col-md4">
                            <label class="layui-form-label layui-form-required">BenchName</label>
                            <div class="layui-input-block">
                                <input required lay-verify="required" id="BenchName" name="BenchName" placeholder="BenchName" type="text" class="layui-input" />
                            </div>
                        </div>
                        <div class="layui-inline  layui-col-md4">
                            <label class="layui-form-label layui-form-required">BenchModel</label>
                            <div class="layui-input-block">
                                <input required lay-verify="required" id="BenchModel" name="BenchModel" placeholder="BenchModel" type="text" class="layui-input" />
                            </div>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <div class="layui-inline  layui-col-md4">
                            <label class="layui-form-label layui-form-required">BenchType</label>
                            <div class="layui-input-block">
                                <select name="BenchType" id="BenchType" lay-search required lay-verify="required">
                                </select>
                            </div>
                        </div>
                        <div class="layui-inline  layui-col-md4">
                            <label class="layui-form-label layui-form-required">Responser</label>
                            <div class="layui-input-block">
                                <select name="Responser" id="Responser" lay-search required lay-verify="required">
                                </select>
                            </div>
                        </div>
                        <div class="layui-inline  layui-col-md4">
                            <label class="layui-form-label layui-form-required">InArea</label>
                            <div class="layui-input-block">
                                <select name="InArea" id="InArea" lay-search required lay-verify="required">
                                </select>
                            </div>
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <div class="layui-inline  layui-col-md12">
                            <label class="layui-form-label layui-form-required">KeyParam</label>
                            <div class="layui-input-block">
                                <textarea id="KeyParam" name="KeyParam" placeholder="KeyParam" type="text" class="layui-textarea"></textarea>
                            </div>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <div class="layui-inline  layui-col-md12">
                            <label class="layui-form-label">Remark</label>
                            <div class="layui-input-block">
                                <textarea id="Remark" name="Remark" placeholder="Remark" type="text" class="layui-textarea"></textarea>
                            </div>
                        </div>
                    </div>


                </div>


                <div class="layui-form-item text-right model-form-footer">
                    <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">Cancel</button>
                    <button class="layui-btn" lay-filter="TestBenchFormSubmit" lay-submit>Save</button>
                </div>
            </form>
        </div>
        <!--EquipList-->
        <div class="layui-tab-item">
            <table class="layui-table" id="tableEquip" lay-filter="tableEquip"></table>
        </div>
        <!--SpotCheck-->
        <div class="layui-tab-item">
            <div class="layui-tab layui-tab-brief">
                <ul class="layui-tab-title">
                    <li class="layui-this">CheckList</li>
                    <li class="layui">CheckRecord</li>
                </ul>
                <div class="layui-tab-content">
                    <div class="layui-tab-item layui-show">
                        <table class="layui-table" id="tableCheckItem" lay-filter="tableCheckItem"></table>
                    </div>
                    <div class="layui-tab-item">
                        <table class="layui-table" id="tableCheckRecord" lay-filter="tableCheckRecord"></table>
                    </div>
                </div>
            </div>

        </div>
    </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="TbOperateBar">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">Edit</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">Del</a>
</script>
<!-- 表单弹窗 -->
<script type="text/html" id="CheckItemDialog">
    <form id="CheckItemForm" lay-filter="CheckItemForm" class="layui-form model-form" style="padding-left: 10px;">

        <div class="layui-form-item">
            <label class="layui-form-label">CheckItem</label>
            <div class="layui-input-block">
                <textarea name="CheckItem" placeholder="Please Input CheckItem" class="layui-textarea"></textarea>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">Remark</label>
            <div class="layui-input-block">
                <textarea name="Remark" placeholder="Please Input Remark" class="layui-textarea"></textarea>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label layui-form-required">SortNo</label>
            <div class="layui-input-block">
                <input name="SortNo" placeholder="Please Input SortNo" class="layui-input" type="number"
                       lay-verType="tips" lay-verify="required" required />
            </div>
        </div>
        <div class="layui-form-item text-right">
            <button class="layui-btn" lay-filter="dictDataEditSubmit" lay-submit>Save</button>
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">Cancel</button>
        </div>
    </form>
</script>

<script>
    layui.use(['layer', 'form', 'table', 'util', 'admin', 'setter', 'tree'], async function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var util = layui.util;
        var admin = layui.admin;
        var setter = layui.setter;
        var tree = layui.tree;

        if (setter.getUser() == null) {
            location.replace(setter.loginPath);
        }
        var thisindex = this.layer.index;
        var ParentData = admin.getLayerData(thisindex);


        // #region basic
        var insTb = ParentData.insTb;
        var treeRender = ParentData.treeRender;
        var seeType = ParentData.seeType;
        var Id = ParentData.Id;
        var detailModel = {};
        var AttachId = "";

        var UserDropHtml = "<option value=''>==Please Select Responser==</option>";
        var BenchTypeDropHtml = "<option value=''>==Please Select BenchType==</option>";
        var InAreaDropHtml = "<option value=''>==Please Select InArea==</option>";

        await admin.req(setter.appServer + 'Resource_Bench/GetInitData', JSON.stringify({}), function (res) {
            if (res.code == 0) {
                for (var i = 0; i < res.data.ResponserList.length; i++) {
                    UserDropHtml = UserDropHtml + "<option value='" + res.data.ResponserList[i].Id + "'>" + res.data.ResponserList[i].RealName + "</option>";
                }
                for (var i = 0; i < res.data.AreaList.length; i++) {
                    InAreaDropHtml = InAreaDropHtml + "<option value='" + res.data.AreaList[i].Id + "'>" + res.data.AreaList[i].SourceName + "</option>";
                }
                for (var i = 0; i < res.data.BenchTypeList.length; i++) {
                    BenchTypeDropHtml = BenchTypeDropHtml + "<option value='" + res.data.BenchTypeList[i].Id + "'>" + res.data.BenchTypeList[i].SourceName + "</option>";
                }
                $('#Responser').append(UserDropHtml);
                $('#BenchType').append(BenchTypeDropHtml);
                $('#InArea').append(InAreaDropHtml);
            }
            else {
                layer.msg(res.data, { icon: 2 })
            }
        }, 'post');

        if (seeType == 1) {
            AttachId = admin.util.uuid(16, 32);
            form.render();
            $('.editli').hide();
        }
        else {
            await admin.req(setter.appServer + 'Resource_Bench/GetDetail', JSON.stringify({ Id: Id }), function (res) {
                detailModel = res.data;
                AttachId = detailModel.AttachId;
                form.render();
                form.val('TestBenchForm', detailModel);
            }, 'post')
        }

        form.on('submit(TestBenchFormSubmit)', function (data) {

            if (seeType == 1) {
                detailModel = data.field;
                detailModel.Id = 0;
                detailModel.AttachId = AttachId;
            }
            else {
                for (x in data.field) {
                    detailModel[x] = data.field[x];
                }
            }
            layer.load(0);
            admin.req(setter.appServer + 'Resource_Bench/Save', JSON.stringify({ strEntity: JSON.stringify(detailModel) }), function (res) {
                layer.closeAll('loading');
                layer.msg("保存成功。", { icon: 1 });
                if (res.code == 0) {
                    try {
                        insTb.reload({}, 'data');
                        treeRender();
                    }
                    catch { }
                    if (seeType == 1) {
                        seeType = 2;
                        detailModel = res.data;
                        Id = res.data.Id;
                        $('#editli').show();
                        insTbCheckItem.reload({ where: { Id: Id } }, true);
                    }
                    else {
                        layer.close(thisindex);
                    }
                }
                else {
                    layer.msg(res.data, { icon: 2 });
                }
            }, 'post');
            return false;
        })
        // #endregion

        // #region spotcheck
        var insTbCheckItem = table.render({
            elem: '#tableCheckItem',
            method: 'post',
            headers: { 'Authorization': setter.getToken() },
            contentType: 'application/json',
            url: layui.setter.appServer + 'Resource_Bench/GetBenchCheckItemList',
            height: 'full-200',
            page: false,
            toolbar: ['<p>',
                '<button lay-event="add" class="layui-btn layui-btn-sm icon-btn"><i class="layui-icon">&#xe654;</i>add</button>&nbsp;',
                //'<button lay-event="edit" class="layui-btn layui-btn-sm layui-btn-warm icon-btn"><i class="layui-icon">&#xe642;</i>edit</button>&nbsp;',
                //'<button lay-event="del" class="layui-btn layui-btn-sm layui-btn-danger icon-btn"><i class="layui-icon">&#xe640;</i>del</button>',
                '</p>'].join(''),
            defaultToolbar: [],
            where: { Id: Id },
            cols: [[
                { type: 'numbers' },
                { field: 'CheckItem', title: 'CheckItem' },
                { title: 'OperateBar', toolbar: '#TbOperateBar', align: 'center', width: 120, minWidth: 120 }
            ]],
            done: function (res, curr, count) {

            }
        });
        table.on('toolbar(tableCheckItem)', function (obj) {
            if (obj.event == 'add') {

            }
        });
        function showCheckItemModel(mData) {
            admin.open({
                type: 1,
                title: (mData ? 'Edit' : 'Add') + 'CheckItem',
                content: $('#CheckItemDialog').html(),
                success: function (layero, dIndex) {
                    // 回显表单数据
                    form.val('CheckItemForm', mData);
                    // 表单提交事件
                    form.on('submit(dictEditSubmit)', function (data) {

                        if (mData) {
                            for (x in data.field) {
                                mData[x] = data.field[x];
                            }
                        }
                        else {
                            mData = data.field;
                            mData.Id = 0;
                        }

                        var loadIndex = layer.load(2);
                        admin.req(setter.appServer + "Sys_DataSource/SaveDataSource", JSON.stringify({ strEntity: JSON.stringify(mData) }), function (res) {

                            layer.close(loadIndex);
                            if (res.code === 0) {
                                layer.close(dIndex);
                                layer.msg('Success', { icon: 1 });
                                insTb.reload();
                            } else {
                                layer.msg(res.msg, { icon: 2 });
                            }
                        }, 'post');
                        return false;
                    });
                }
            });
        }
        // #endregion


    });
</script>
